package cn.lili.controller.recharge;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.order.trade.entity.vo.RechargeQueryVO;
import cn.lili.modules.payment.entity.dos.Recharge;
import cn.lili.modules.wallet.service.RechargeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 买家端,预存款充值记录接口
 *
 * @author pikachu
 * @since 2020/11/16 10:07 下午
 */
@RestController
@Api(tags = "充值接口")
@RequestMapping("/payment/recharge")
@Validated
@RequiredArgsConstructor
public class RechargeController {

    private final RechargeService rechargeService;

    @ApiOperation(value = "分页获取预存款充值记录")
    @GetMapping
    public ResultMessage<Page<Recharge>> getByPage(PageVO page, RechargeQueryVO rechargeQueryVO) {
        AuthUser currentUser = UserContext.getCurrentUser();
        if (currentUser != null) {
            switch (currentUser.getScene()) {
                case MEMBER -> rechargeQueryVO.setUserId(currentUser.getId());
                case STORE, SUPPLIER -> rechargeQueryVO.setUserId(UserContext.getCurrentId());
                default -> {
                }
            }
        }
        //构建查询 返回数据
        Page<Recharge> rechargePage = rechargeService.rechargePage(page, rechargeQueryVO);
        return ResultUtil.data(rechargePage);
    }

    @PreventDuplicateSubmissions
    @PostMapping
    @ApiOperation(value = "创建余额充值订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "充值金额", required = true, dataType = "double", paramType = "query")
    })
    public ResultMessage<Recharge> create(@Max(value = 10000, message = "充值金额单次最多允许充值10000元") @NotNull(message = "金额不能为空") Double price) {
        Recharge recharge = this.rechargeService.recharge(price);
        return ResultUtil.data(recharge);
    }

    @GetMapping("getRechargeStatus")
    @ApiOperation(value = "查询充值订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "充值订单号", required = true, dataType = "String", paramType = "query")
    })
    public ResultMessage<Recharge> getRechargeStatus(@NotNull(message = "订单号不能为空") String sn) {
        Recharge recharge = this.rechargeService.getRechargeBySn(sn);
        return ResultUtil.data(recharge);
    }



}
